﻿Imports System.Data.Linq
Public Class CDanhMucController
    Dim objDataContext As QLyNganSachDataContext

    Public Sub New()
        objDataContext = New QLyNganSachDataContext
    End Sub
    Public Sub New(ByVal _obj As QLyNganSachDataContext)
        objDataContext = _obj
    End Sub
    Public Function SelectAllRecord() As IQueryable(Of tblDanhMuc)
        Try
            Return (From d In objDataContext.tblDanhMucs
                    Order By d.Ma
                    Select d)
        Catch ex As Exception
            Return Nothing
        End Try
    End Function
    Public Function SelectAllCode() As Array
        Try
            Return (From dm In objDataContext.tblDanhMucs
                     Select dm.Ma).ToArray()
        Catch ex As Exception
            Return Nothing
        End Try
    End Function
    Public Function SelectRecordByID(ByVal _id As Integer) As tblDanhMuc
        Try
            Return (From r In objDataContext.tblDanhMucs
                    Where r.ID_DanhMuc = _id
                    Select r).Single
        Catch ex As Exception
            Throw ex
        End Try
    End Function
    Public Function SelectRecordByCode(ByVal _code As String) As tblDanhMuc
        Try
            Return (From r In objDataContext.tblDanhMucs
                    Where r.Ma = _code
                    Select r).Single
        Catch ex As Exception
            Return Nothing
        End Try
    End Function
    Public Function InsertRecord(ByVal LocalTable As tblDanhMuc) As Integer
        Dim id As Integer = -1
        Try
            objDataContext.tblDanhMucs.InsertOnSubmit(LocalTable)
            objDataContext.SubmitChanges()
            id = LocalTable.ID_DanhMuc
        Catch ex As Exception
            Throw ex
        End Try
        Return id
    End Function
    Public Sub UpdateRecord(ByVal LocalTable As tblDanhMuc)
        Try
            objDataContext.tblDanhMucs.Attach(LocalTable)
            objDataContext.Refresh(RefreshMode.KeepCurrentValues, LocalTable)
            objDataContext.SubmitChanges(ConflictMode.ContinueOnConflict)
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
    Public Sub UpdateRecordWithoutAttach(ByVal LocalTable As tblDanhMuc)
        Try
            objDataContext.Refresh(RefreshMode.KeepCurrentValues, LocalTable)
            objDataContext.SubmitChanges(ConflictMode.ContinueOnConflict)
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Public Sub DeleteRecord(ByVal _id As Integer)
        Try
            objDataContext.tblDanhMucs.DeleteOnSubmit( _
              (From r In objDataContext.tblDanhMucs _
               Where r.ID_DanhMuc = _id).Single)
            objDataContext.SubmitChanges()
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
    Public Function CheckIfExists(ByVal _id As String, ByVal _ma As String) As Boolean
        Return objDataContext.tblDanhMucs.Any(Function(d) d.Ma = _ma And d.ID_DanhMuc <> _id)
    End Function
End Class
